﻿/*
    Copyright (c) 2007 - 2009, Carlos Guzmán Álvarez

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
    are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice, 
          this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright notice, 
          this list of conditions and the following disclaimer in the documentation and/or 
          other materials provided with the distribution.
        * Neither the name of the author nor the names of its contributors may be used to endorse or 
          promote products derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Framework.Net.Xmpp.Core;
using Framework.Net.Xmpp.Serialization.InstantMessaging.Client;
using Framework.Net.Xmpp.Serialization.InstantMessaging.Client.Presence;
using Framework.Net.Xmpp.Serialization.InstantMessaging.Roster;

namespace Framework.Net.Xmpp.InstantMessaging
{
    /// <summary>
    /// Represents a <see cref="XmppRoster"/> contact.
    /// </summary>
	public sealed class XmppContact : INotifyPropertyChanged
    {
        #region · Static Members ·

        private static sbyte DefaultPresencePriorityValue = -99;

        #endregion

        #region · INotifyPropertyChanged Members ·

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region · Fields ·

        private string                      name;
        private string                      displayName;
        private XmppJid                     contactId;
        private XmppSession                 session;
        private XmppContactSubscriptionType subscription;
        private List<XmppContactResource>   resources;
        private List<string>                groups;
        private object                      syncObject;

        #endregion

        #region · Properties ·

        /// <summary>
        /// Gets the contact id.
        /// </summary>
        /// <value>The contact id.</value>
        public XmppJid ContactId
        {
            get { return this.contactId; }
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return this.name; }
            private set
            {
                if (this.name != value)
                {
                    this.name = value;

                    this.NotifyPropertyChanged("Name");
                }
            }
        }

        /// <summary>
        /// Gets the groups where the contact is included in.
        /// </summary>
        /// <value>The groups.</value>
        public List<string> Groups
        {
            get 
            {
                if (this.groups == null)
                {
                    this.groups = new List<string>();
                }

                return this.groups;
            }
        }

        /// <summary>
        /// Gets the contact Display Name
        /// </summary>
        public string DisplayName
        {
            get { return this.displayName; }
            set
            {
                if (this.displayName != value)
                {
                    if (!String.IsNullOrEmpty(value))
                    {
                        this.displayName = value;
                    }
                    else
                    {
                        this.displayName = this.contactId.UserName;
                    }

                    this.Update();

                    this.NotifyPropertyChanged("DisplayName");
                }
            }
        }

        /// <summary>
        /// Gets the list available resources.
        /// </summary>
        /// <value>The resources.</value>
        public IEnumerable<XmppContactResource> Resources
        {
            get
            {
                if (this.resources == null)
                {
                    this.resources = new List<XmppContactResource>();
                }

                foreach (XmppContactResource resource in this.resources)
                {
                    yield return resource;
                }
            }
        }

        /// <summary>
        /// Gets or sets the subscription.
        /// </summary>
        /// <value>The subscription.</value>
        public XmppContactSubscriptionType Subscription
        {
            get { return this.subscription; }
            set
            {
                if (this.subscription != value)
                {
                    this.subscription = value;

                    this.NotifyPropertyChanged("Subscription");
                }
            }
        }

        /// <summary>
        /// Gets the presence.
        /// </summary>
        /// <value>The presence.</value>
        public XmppContactResource Resource
        {
            get { return this.GetResource(); }
        }

        /// <summary>
        /// Gets the presence.
        /// </summary>
        /// <value>The presence.</value>
        public XmppContactPresence Presence
        {
            get { return this.GetResource().Presence; }
        }

        #endregion

        #region · Constructors ·

        /// <summary>
        /// Initializes a new instance of the <see cref="T:XmppContact"/> class.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <param name="contactId">The contact id.</param>
        /// <param name="name">The name.</param>
        /// <param name="subscription">The subscription.</param>
        /// <param name="groups">The groups.</param>
        internal XmppContact(XmppSession session, string contactId, string name, XmppContactSubscriptionType subscription, ICollection groups)
		{
            this.session    = session;
            this.syncObject = new object();
            this.contactId  = new XmppJid(contactId);
            this.groups     = new List<string>();
            this.resources  = new List<XmppContactResource>();

            this.RefreshData(name, subscription, groups);
            this.AddDefaultResource();
        }

        #endregion

        #region · Methods ·

        /// <summary>
        /// Reads the original avatar
        /// </summary>
        /// <returns></returns>
        public Stream ReadOriginalAvatar()
        {
            return new MemoryStream(this.session.Configuration.ReadOriginalAvatar(this.ContactId.ToString()));
        }

        /// <summary>
        /// Reads the online state avatar
        /// </summary>
        /// <returns></returns>
        public Stream ReadOnlineAvatar()
        {
            return new MemoryStream(this.session.Configuration.ReadOnlineAvatar(this.ContactId.ToString()));
        }

        /// <summary>
        /// Adds to group.
        /// </summary>
        /// <param name="groupName">Name of the group.</param>
        public void AddToGroup(string groupName)
        {
            IQ          iq      = new IQ();
            RosterQuery query   = new RosterQuery();
            RosterItem  item    = new RosterItem();

            this.groups.Add(groupName);

            iq.Type = IQType.Set;

            item.Jid            = this.ContactId.BareIdentifier;
            item.Name           = this.Name;
            item.Subscription   = (RosterSubscriptionType)this.Subscription;

            item.Groups.AddRange(this.groups.ToArray());

            query.Items.Add(item);
            iq.Items.Add(query);

            this.session.Send(iq);
        }

        /// <summary>
        /// Updates the contact data.
        /// </summary>
        public void Update()
        {
            IQ          iq      = new IQ();
            RosterQuery query   = new RosterQuery();
            RosterItem  item    = new RosterItem();

            iq.Type = IQType.Set;

            item.Jid    = this.ContactId.BareIdentifier;
            item.Name   = this.DisplayName;
            item.Subscription = (RosterSubscriptionType)this.Subscription;

            item.Groups.AddRange(this.groups.ToArray());

            query.Items.Add(item);
            iq.Items.Add(query);

            this.session.Send(iq);
        }

        /// <summary>
        /// Request subscription to he presence of the contanct
        /// </summary>
        public void RequestSubscription()
        {
            this.session.Presence.RequestSubscription(this.ContactId);
        }

        /// <summary>
        /// Block contact
        /// </summary>
        public void Block()
        {
#warning TODO: Implement
        }

        /// <summary>
        /// Unblock contact.
        /// </summary>
        public void Unblock()
        {
#warning TODO: Implement
        }

        #endregion

        #region · Overriden Methods ·

        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            return this.ContactId.ToString();
        }

        #endregion

        #region · Internal Methods ·

        internal void AddDefaultResource()
        {
            XmppContactResource contactResource = new XmppContactResource(this.session, this, this.ContactId);
            Presence            defaultPresence = new Presence();

            // Add a default resource
            defaultPresence.TypeSpecified   = true;
            defaultPresence.From            = this.contactId.BareIdentifier;
            defaultPresence.Type            = PresenceType.Unavailable;

            defaultPresence.Items.Add(DefaultPresencePriorityValue);

            contactResource.Update(defaultPresence);

            this.resources.Add(contactResource);
        }

        internal void RefreshData(string name, XmppContactSubscriptionType subscription, ICollection groups)
        {
            this.Name = ((name == null) ? String.Empty : name);

            if (!String.IsNullOrEmpty(this.Name))
            {
                this.displayName = this.name;
            }
            else
            {
                this.displayName = this.contactId.UserName;
            }

            this.Groups.Clear();

            foreach (string group in groups)
            {
                this.Groups.Add(group);
            }

            if (this.Groups.Count == 0)
            {
                this.Groups.Add("Contacts");
            }

            this.NotifyPropertyChanged("DisplayName");
            this.NotifyPropertyChanged("Groups");
        }

        internal void UpdatePresence(XmppJid jid, Presence presence)
        {
            lock (this.syncObject)
            {
                if (presence.TypeSpecified && presence.Type == PresenceType.Unavailable)
                {
                    // If the presence comes without a resource and presence types is unavailable
                    // let the contact go offline
                    this.Presence.PresenceStatus = XmppPresenceState.Offline;
                }
                else
                {
                    XmppContactResource resource = this.Resources.Where(contactResource => contactResource.ResourceId.ToString() == presence.From).SingleOrDefault();

                    if (resource == null)
                    {
                        resource = new XmppContactResource(this.session, this, jid);
                        this.resources.Add(resource);
                    }

                    resource.Update(presence);
                }

                this.NotifyPropertyChanged("Presence");
                this.NotifyPropertyChanged("Resource");
            }
        }

        #endregion

        #region · Private Methods ·

        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private XmppContactResource GetResource()
        {
            var q = from resource in this.resources
                    orderby resource.Presence.Priority descending
                    select resource;

            return q.FirstOrDefault();
        }

        #endregion
    }
}
